﻿using System;
using FluentValidation;
using FluentValidation.Results;
using GiveAQuiz.Web.Helpers;
using GiveAQuiz.Web.Models;

namespace GiveAQuiz.Web.Validation
{
  /// <summary>
  /// Validate the StartQuizViewModel
  /// </summary>
  public class StartQuizValidator : AbstractValidator<StartQuizViewModel>
  {
    /// <summary>
    /// Creates a new instance of the StartQuizValidator
    /// </summary>
    public StartQuizValidator()
    {
      RuleFor(m => m.QuizDefinitionId)
        .GreaterThan(0)
        .WithMessage("The quiz definition is required");

      RuleFor(m => m.QuizName)
        .NotBlank()
        .WithMessage("You must enter a quiz name");

      RuleFor(m => m.Passphrase)
        .NotBlank()
        .WithMessage("You must enter a quiz passphrase");

      RuleFor(m => m.StartDate)
        .GreaterThan(DateTime.MinValue)
        .WithMessage("You must select a start date");

      RuleFor(m => m.EndDate)
        .GreaterThan(DateTime.MinValue)
        .WithMessage("You must select an end date");

      // make sure the end date is after the start date
      Custom(m =>
        m.EndDate < m.StartDate
          ? new ValidationFailure("EndDate", "You must enter an end date after the start date")
          : null);
    }
  }
}
